using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using PostSharp.Laos;
using System.Threading;

namespace Coriander.Xmpp.Logging.Specialized.PostSharp.Attributes
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public sealed class TraceAttribute : OnMethodBoundaryAspect
    {
        /// <summary>
        /// Handles method entry event.
        /// </summary>
        /// <param name="eventArgs"></param>
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
            String operationColumn = FormatColumn(
                " [" + eventArgs.Method.Name + "]", 25
            );

            Trace.WriteLine(
                String.Format(
                    "[{0}][{1}] {2} [thread: {3}]",
                    DateTime.UtcNow.ToString("yyyy/MM/dd hh:mm:ss"),
                    DateTime.UtcNow.ToBinary(),
                    operationColumn,
                    Thread.CurrentThread.ManagedThreadId.ToString("00")
                )
            );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="width"></param>
        String FormatColumn(String value, Int32 width)
        {
            String formatString = String.Format("{{0,-{0}:g}}", width);

            return String.Format(formatString, value);
        }
    }
}
